import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

public class LatexSaver {
    static public void saveLatex(String latex, String filename) {
        // save latex to filename
        try {
            // 创建FileWriter来写入LaTeX文件
            FileWriter fileWriter = new FileWriter(filename);
            PrintWriter printWriter = new PrintWriter(fileWriter);

            // 写入LaTeX文档的基本结构
            printWriter.println("\\documentclass{article}");
            printWriter.println("\\begin{document}");

            // 这里添加您的Java生成的内容
            printWriter.println("\\[\n" + latex + "\n\\]");

            // 关闭文档
            printWriter.println("\\end{document}");

            // 关闭写入器
            printWriter.close();
            System.out.println("LaTeX save success, file: " + filename);

        } catch (IOException e) {
            System.out.println("generate LaTeX error" + e.getMessage());
        }
    }
}
